﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Enterprise.Service.Role;
using Enterprise.Model.Role;
using Enterprise.Service.Doc;
using Enterprise.Model.Doc;
using Enterprise.Infrastructure;
namespace Enterprise.UI.Web.Modules.Doc
{
    public partial class ClassManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindRoles();
                bindGrid();
            }
        }

        #region "数据初始化"

        private void bindRoles()
        {
            RoleService rs = new RoleService();
            t_pubroles.DataSource= rs.RoleList().OrderBy(s=>s.ROLEID).ToList();
            t_pubroles.DataValueField = "ROLEID";
            t_pubroles.DataTextField = "RNAME";
            t_pubroles.DataBind();

            t_viewroles.DataSource = rs.RoleList().OrderBy(s=>s.ROLEID).ToList();
            t_viewroles.DataValueField = "ROLEID";
            t_viewroles.DataTextField = "RNAME";
            t_viewroles.DataBind();            
        }

        private void bindGrid()
        {
            DocClassService dcs = new DocClassService();
            gv.DataSource = dcs.GetAllList();
            gv.DataBind();
        }

        #endregion

       

        protected string getListItemsString(ListBox listbox)
        {
            string str = "";
            foreach (ListItem item in listbox.Items)
            {
                if(!string.IsNullOrEmpty(item.Value)&&item.Selected)
                str += item.Value+",";
            }
            if(str.Length>0)
                str = str.Remove(str.Length - 1);
            return str;
        }

        protected void gv_SelectedIndexChanged(object sender, EventArgs e)
        {
            DocClassEntity model = new DocClassEntity();
            DocClassService dcs = new DocClassService();
            decimal? _cid= gv.SelectedRow.Cells[0].Text.ToDecimal().Value;
            model = dcs.GetModel(_cid.HasValue?_cid.Value:0);
            t_pubroles.ClearSelection();
            t_viewroles.ClearSelection();
            if (model.CLASSID > 0)
            {
                t_classid.Text = model.CLASSID.ToDecimal().ToString();
                t_classname.Text = model.CLASSNAME;
                t_orderby.Text = model.CLASSORDERBY.ToDecimal().ToString();
                t_classflag.Text = model.CLASSFLAG.ToDecimal().ToString();
                
                if (string.IsNullOrEmpty(model.CLASSPUBROLES))
                    t_pubroles.SelectedValue = "";
                else
                {
                    string[] ss = model.CLASSPUBROLES.Split(',');
                    foreach (var s in ss)
                    {
                        if (t_pubroles.Items.FindByValue(s) != null)
                            t_pubroles.Items.FindByValue(s).Selected = true;
                    }
                }

                if (string.IsNullOrEmpty(model.CLASSVIEWROLES))
                    t_viewroles.SelectedValue = "";
                else
                {
                    string[] ss = model.CLASSVIEWROLES.Split(',');
                    foreach (var s in ss)
                    {
                        if(t_viewroles.Items.FindByValue(s)!=null)
                            t_viewroles.Items.FindByValue(s).Selected = true;
                    }
                }

            }
        }

        protected void BtnAdd_Click(object sender, EventArgs e)
        {
            //新增数据
            DocClassEntity model = new DocClassEntity();
            DocClassService dcs = new DocClassService();
            model.CLASSNAME = t_classname.Text;
            model.CLASSORDERBY = t_orderby.Text.ToDecimal();
            model.CLASSFLAG = t_classflag.Text.ToDecimal();
            model.CLASSPUBROLES = getListItemsString(t_pubroles);
            model.CLASSVIEWROLES = getListItemsString(t_viewroles);
            dcs.Add(model);
            Response.Redirect("classManage.aspx", true);
        } 
        
        protected void BtnSave_OnClick(object sender, EventArgs e)
        {
            //保存数据
            
            DocClassEntity model = new DocClassEntity();
            DocClassService dcs = new DocClassService();            
            model.CLASSID = t_classid.Text.ToDecimal().Value;
            model.CLASSNAME = t_classname.Text;
            model.CLASSORDERBY = t_orderby.Text.ToDecimal();
            model.CLASSFLAG = t_classflag.Text.ToDecimal();
            model.CLASSPUBROLES = getListItemsString(t_pubroles);
            model.CLASSVIEWROLES = getListItemsString(t_viewroles);
            dcs.Update(model);
            Response.Redirect("classManage.aspx",true);
        }

        protected void BtnDelete_Click(object sender, EventArgs e)
        {
            //删除栏目             
            DocClassService dcs = new DocClassService();
            decimal _cid = t_classid.Text.ToDecimal().Value;
            if (!dcs.hasArticle(_cid))
            {
                dcs.Delete(_cid);
            }
            else
            {
                Utility.ShowMsg(this.Page, "这个栏目下还有文章无法删除！请先删除该栏目下所有文章之后再进行此操作");
            }
            Response.Redirect("classManage.aspx", true);
        }
    }
}